/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/***************************************************************************
 * Name: service_field.h
 *
 * Purpose: field template define
 *
 * Developer:
 *   wen.gu , 2021-05-10
 *
 * TODO:
 *
 ***************************************************************************/

/******************************************************************************
 **    INCLUDES
 ******************************************************************************/
#ifndef __SERVICE_FIELD_H__
#define __SERVICE_FIELD_H__

#include "serializer.h"
#include "deserializer.h"
#include "message.h"
#include "object_publisher.h"
/******************************************************************************
 **    MACROS
 ******************************************************************************/


/******************************************************************************
 **    TYPE DEFINITIONS
 ******************************************************************************/



/******************************************************************************
 **    CLASSES/FUNCTIONS DEFINITIONS
 ******************************************************************************/
class ServiceProvider;


template<typename SimpleType, FieldId _field_id, bool has_getter, bool has_setter, bool has_notifier>
class ServiceField: public ObjectPublisher<SimpleType>
{
    
public:
    using FieldGetterHandler = std::function<bool(SimpleType&)>;
    using FieldSetterHandler = std::function<bool(const SimpleType&)>;    
private:
    SimpleType data_;
public:
    ServiceField(ServiceProvider::ServiceProviderPtr sp, SimpleType default_val, FieldGetterHandler getter, FieldSetterHandler setter)
    :ObjectPublisher<SimpleType>(sp, MAKE_FIELD_ID(_field_id)),
    data_(default_val)
    {
        /** todo something */

        if (has_setter)
        {
            provider_->RegisterReceiveHandler(MessageType::kFieldSet, _field_id, [this, setter](MessagePtr msg)
            {
                SimpleType val;
                DeserializerPtr desp = provider_->GetDeserializer(msg->payload());
                if (!desp->Deserialize(val))
                {
                    //todo something
                }
                else
                {
                    if (setter(val))
                    {
                        Update(val);
                    }
                }
            });
        }

        if (has_getter)
        {
            provider_->RegisterReceiveHandler(MessageType::kFieldGet, _field_id, [this, getter](MessagePtr msg)
            {
   
                if (getter(data_))
                {/** todo, is need call Update? */                    
                    Publish(data_, MessageType::kFieldGetAck);
                }
                
            });        
        }

    }

public:
    void Update(const SimpleType& value)
    {
        data_ = value;

        if (has_notifier)
        {
            Publish(data_, MessageType::kFeildNotify);
        }

    }

public:
    bool HasSetter() const { return has_setter; }
    bool HasGetter() const { return has_getter; }
    bool Hasnotifier() const { return has_notifier; }


};

#endif /** !__SERVICE_FIELD_H__ */

